package com.ktgj.customer.service.impl;

import java.util.Date;
import java.util.List;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.vo.MallCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.MallCartMapper;
import com.ktgj.customer.domain.MallCart;
import com.ktgj.customer.service.IMallCartService;

/**
 * 购物车Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@Service
public class MallCartServiceImpl implements IMallCartService 
{
    @Autowired
    private MallCartMapper mallCartMapper;

    /**
     * 查询购物车
     * 
     * @param cartId 购物车ID
     * @return 购物车
     */
    @Override
    public MallCart selectMallCartById(Long cartId)
    {
        return mallCartMapper.selectMallCartById(cartId);
    }

    /**
     * 查询购物车列表
     * 
     * @param mallCart 购物车
     * @return 购物车
     */
    @Override
    public List<MallCart> selectMallCartList(MallCart mallCart)
    {
        return mallCartMapper.selectMallCartList(mallCart);
    }

    /**
     * 新增购物车
     * 
     * @param mallCart 购物车
     * @return 结果
     */
    @Override
    public int insertMallCart(MallCart mallCart)
    {
        mallCart.setCreateTime(DateUtils.getNowDate());
        return mallCartMapper.insertMallCart(mallCart);
    }

    /**
     * 修改购物车
     * 
     * @param mallCart 购物车
     * @return 结果
     */
    @Override
    public int updateMallCart(MallCart mallCart)
    {
        mallCart.setUpdateTime(DateUtils.getNowDate());
        return mallCartMapper.updateMallCart(mallCart);
    }

    /**
     * 批量删除购物车
     * 
     * @param cartIds 需要删除的购物车ID
     * @return 结果
     */
    @Override
    public int deleteMallCartByIds(Long[] cartIds)
    {
        return mallCartMapper.deleteMallCartByIds(cartIds);
    }

    /**
     * 删除购物车信息
     * 
     * @param cartId 购物车ID
     * @return 结果
     */
    @Override
    public int deleteMallCartById(Long cartId)
    {
        return mallCartMapper.deleteMallCartById(cartId);
    }

    //购物车
    @Override
    public List<MallCartVO> mallCartList(Long customerId, String openId) {
        MallCart cart = new MallCart();
        cart.setCustomerId(customerId);
        if (openId != null)
            cart.setOpenId(openId);
        List<MallCart> mallCarts = selectMallCartList(cart);
        if (mallCarts != null && mallCarts.size() > 0) {
            List<MallCartVO> mallCartVOS = null;
            if (openId != null)
                mallCartVOS = mallCartMapper.mallCartList(customerId, openId);
            else
                mallCartVOS = mallCartMapper.mallCartList(customerId, null);
            mallCartVOS.forEach(item -> {
                if (item.getDeleteTime() != null)
                    item.setOffTheShelf(true);
                else
                    item.setOffTheShelf(false);
            });
            return mallCartVOS;
        } else {
            cart.setCreateTime(new Date());
            cart.setUpdateTime(new Date());
            insertMallCart(cart);
            List<MallCartVO> mallCartVOS = null;
            if (openId != null)
                mallCartVOS = mallCartMapper.mallCartList(customerId, openId);
            else
                mallCartVOS = mallCartMapper.mallCartList(customerId, null);
            mallCartVOS.forEach(item -> {
                if (item.getDeleteTime() != null)
                    item.setOffTheShelf(true);
                else
                    item.setOffTheShelf(false);
            });
            return mallCartVOS;
        }
    }

    /**
     * 购物车商品数量
     * @param openId
     * @return
     */
    @Override
    public Long mallCartNum(Long customerId, String openId) {
        Long number = 0L;
        List<MallCartVO> mallCartVOS = null;
        if (openId != null)
            mallCartVOS = mallCartMapper.mallCartList(customerId, openId);
        else
            mallCartVOS = mallCartMapper.mallCartList(customerId, null);
        if (mallCartVOS != null && mallCartVOS.size() > 0) {
            number = Long.valueOf(mallCartVOS.size());
        }
        return number;
    }
}
